package com.kurumi.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.pojo.MedicalHomepage;
import com.kurumi.service.SyncDataService;
import com.kurumi.util.DateUtil;
import com.kurumi.util.FileUtil;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.MedicalHomepageUtil;
import com.kurumi.util.StringUtil;

@RequestMapping("/sync")
@Controller
public class SyncDataController {

	@Autowired
	private SyncDataService syncDataService;
	
	
	@ResponseBody
	@RequestMapping("/deleteRepeatData")
	public int deleteRepeatData(){
		List<String> repeatJsonGuid = syncDataService.getRepeatDataJson();
		for (String guid : repeatJsonGuid) {
			syncDataService.deleteRepeatJsonByGuid(guid);
		}
		return repeatJsonGuid.size();
	}
	
	@ResponseBody
	@RequestMapping("/syncData")
	public List<String> syncData(){
		
		List<String> guidList = syncDataService.getGuid();
//		List<String> basiclist = new ArrayList<>();
//		List<String> curelist = new ArrayList<>();
//		List<String> diaglist = new ArrayList<>();
		int count = 1;
		MedicalHomepage homepage = null;
		for (String visitGuid : guidList) {
			String json = syncDataService.getJsonByGuid(visitGuid);
			homepage=MedicalHomepageUtil.getMedicalHomepage(json);
			if(homepage!=null){
				syncDataService.insrtMedicalHomepage(homepage);
				System.out.println("处理数据数量:"+count);
				count++;
			}
			//System.out.println(json);
//			Map<String,Object> map = JsonUtil.jsonToPojo(json, Map.class);
//			if(MapUtils.isNotEmpty(map)){
//				Map<String,Object> basicMap = (Map<String, Object>) MapUtils.getObject(map, "basicInfo");
//				if(MapUtils.isEmpty(basicMap)){
//					basiclist.add(visitGuid);
//				}
//				
//				Map<String,Object> cureInfoMap = (Map<String, Object>) MapUtils.getObject(map, "cureInfo");
//				if(MapUtils.isEmpty(cureInfoMap)){
//					curelist.add(visitGuid);
//				}
//				
//				
//				Map<String,Object> diagInfoMap = (Map<String, Object>) MapUtils.getObject(map, "diseaseDiagInfo");
//				if(MapUtils.isEmpty(diagInfoMap)){
//					diaglist.add(visitGuid);
//				}
//				
//				
//			}
			
		}
		
		//基本信息数量:[0b4ac0b768dc4f939402b551c1d05d87, 1060ba6b2685419093eeec83649306c5, 5d213ebf95a34666a2a586e136eceaaa, 99b93fbc517f4a8db95d98a3141377ca, c4c1ffec2c7040d7bbb7a820992c50ec, fd5ff75fb9f642848be7010a3ba4548b]
		
//		System.out.println("基本信息数量:"+basiclist);
//		System.out.println("治疗信息数量:"+curelist);
//		System.out.println("诊断信息数量:"+curelist);
		
		
		
		return null;
		
	}
	
	
	@ResponseBody
	@RequestMapping("/insertJson")
	public void insertJson() throws IOException{
		String visitGuid = "0b4ac0b768dc4f939402b551c1d05d87";
		String filePath = "D://publics_qf/medical_record/jsons/"+ StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";;
		String versionFilePath = filePath+"version\\";
		String fileName = visitGuid + ".json";
		String versionFileName = "0b4ac0b768dc4f939402b551c1d05d87-201911121051000038.json";
		String newVersionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
		
		String json = FileUtil.readFile(filePath, fileName);
		String versionJson = FileUtil.readFile(versionFilePath,versionFileName);
		System.out.println(versionJson);
		
		Map<String,Object> versionMap = JsonUtil.jsonToPojo(versionJson, Map.class);
		
		Map<String,Object> map = JsonUtil.jsonToPojo(json, Map.class);
		
		versionMap.putAll(map);
		
		String saveJson = JsonUtil.objectToJson(versionMap);
		
		syncDataService.insertJson(saveJson);
		
		FileUtil.createOrEditFile(saveJson, filePath, fileName);
		FileUtil.createOrEditFile(saveJson, versionFilePath, newVersionFileName);
		
	}
	
//	public static void main(String[] args) throws IOException {
//		String visitGuid = "0b4ac0b768dc4f939402b551c1d05d87";
//		String filePath = "D://publics_qf/medical_record/jsons/"+ StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";;
//		String versionFilePath = filePath+"version\\";
//		String fileName = visitGuid + ".json";
//		String versionFileName = "0b4ac0b768dc4f939402b551c1d05d87-201911121051000038.json";
//		String newVersionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
//		
//		String json = FileUtil.readFile(filePath, fileName);
//		String versionJson = FileUtil.readFile(versionFilePath,versionFileName);
//		System.out.println(versionJson);
//		
//		Map<String,Object> versionMap = JsonUtil.jsonToPojo(versionJson, Map.class);
//		
//		Map<String,Object> map = JsonUtil.jsonToPojo(json, Map.class);
//		
//		versionMap.putAll(map);
//		
//		String saveJson = JsonUtil.objectToJson(versionMap);
//		
//		FileUtil.createOrEditFile(saveJson, filePath, fileName);
//		FileUtil.createOrEditFile(saveJson, versionFilePath, newVersionFileName);
//	}
//	
	
	
	
}
